﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SL8.SL.Data.Helpers;

namespace SL8.SL.Data.Sql
{
    public abstract class DALBase : IDAL
    {
        public abstract ISqlProvider SqlProvider { get; }

        public void Add<T>(T t, Action<Result<T>> onComplete) where T : new()
        {
            string sql = SqlHelper.GetAddSql(t);
            this.SqlProvider.Excute<T>(sql, onComplete);
        }

        public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expr, Action<Result<T>> onComplete) where T : new()
        {
            string sql = SqlHelper.GetDeleteSql(expr);
            this.SqlProvider.Excute<T>(sql, onComplete);
        }

        public void Update<T>(T t, System.Linq.Expressions.Expression<Func<T, object>> updater, System.Linq.Expressions.Expression<Func<T, bool>> condition, Action<Result<T>> onComplete) where T : new()
        {
            string sql = SqlHelper.GetUpdateSql<T>(t, updater, condition);
            this.SqlProvider.Excute<T>(sql, onComplete);
        }

        public void Select<T>(SearchCondition<T> searchCondition, Action<Result<T>> onComplete) where T : new()
        {
            string sql = SqlHelper.GetSearchConditionSql<T>(searchCondition);
            this.SqlProvider.Excute<T>(sql, onComplete);
        }

        public void Select<T>(SearchCondition<T> searchCondition, System.Linq.Expressions.Expression<Func<T, object>> selector, Action<Result<T>> onComplete) where T : new()
        {
            string sql = SqlHelper.GetSearchConditionSql(searchCondition, selector);
            this.SqlProvider.Excute<T>(sql, onComplete);
        }

        public void Excute<T>(string sql, Action<Result<T>> onComplete) where T : new()
        {
            this.SqlProvider.Excute<T>(sql, onComplete);
        }

        public void GetRecordCount<T>(System.Linq.Expressions.Expression<Func<T, bool>> expr, Action<Result<RecordCount>> onComplete) where T : new()
        {
            string sql = SqlHelper.GetRecordCountSql(expr);
            this.SqlProvider.Excute<RecordCount>(sql, onComplete);
        }
    }
}
